package com.tywj.lib.social

import android.app.Activity
import android.os.Bundle
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.core.common.util.ToastUtil

/**
 * <pre>
 *     author : June Yang
 *     time   : 2020/5/16
 *     desc   : 微信登录、分享、支付结果处理Activity
 *     version: 1.0.0
 * </pre>
 */
open class WeChatHandlerActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        SocialUtil.wxApi.handleIntent(intent, object : IWXAPIEventHandler {
            override fun onReq(req: BaseReq) {}

            override fun onResp(resp: BaseResp) {
                when (resp.type) {
                    //微信分享
                    ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX -> handleShareResponse(resp)
                    //微信登录
                    ConstantsAPI.COMMAND_SENDAUTH -> handleLoginResponse(resp)
                    //微信支付
                    ConstantsAPI.COMMAND_PAY_BY_WX -> handlePayResponse(resp)
                    //小程序返回值
                    ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM -> {

                        val launchMiniProResp = resp as WXLaunchMiniProgram.Resp

                        val extraData =
                            launchMiniProResp.extMsg //对应小程序组件 <button open-type="launchApp"> 中的 app-parameter 属性
//                        ToastUtil.shortToast("小程序返回的data:$extraData")
                    }
                }
                finish()
            }
        })
    }

    private fun handleShareResponse(resp: BaseResp) {
        if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
            SocialUtil.share.callback?.onSuccess()
        } else {
            SocialUtil.share.callback?.onError()
        }
        SocialUtil.share.callback = null
    }

    private fun handleLoginResponse(resp: BaseResp) {
        var result: Int
        when (resp.errCode) {

            //成功
            BaseResp.ErrCode.ERR_OK -> {
                result =
                    R.string.social_login_errcode_author_success
                //类型转换
                val code = (resp as SendAuth.Resp).code

                if (code.isNotEmpty()) {
                    SocialUtil.login.weChatLoginCallBack?.onSuccess(code)
                } else {

                    result =
                        R.string.social_login_errcode_unsupported
                    SocialUtil.login.weChatLoginCallBack?.onError(resp.errCode, ResourcesUtil.getString(result))
                }
            }

            //失败（取消、不支持，返回等等）
            BaseResp.ErrCode.ERR_USER_CANCEL -> {
                result =
                    R.string.social_login_errcode_cancel
            }
            BaseResp.ErrCode.ERR_AUTH_DENIED -> {
                result = R.string.social_login_errcode_deny
            }
            BaseResp.ErrCode.ERR_UNSUPPORT -> {
                result =
                    R.string.social_login_errcode_unsupported
            }
            else -> {
                result =
                    R.string.social_login_errcode_unknown
            }
        }
        try {
            ToastUtil.shortToast(result)
        } catch (e: Exception) {
        }

        SocialUtil.login.weChatLoginCallBack = null
    }

    private fun handlePayResponse(resp: BaseResp) {
        if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
            SocialUtil.pay.callback?.onSuccess()
        } else {
            SocialUtil.pay.callback?.onError()
        }
        SocialUtil.pay.callback = null
    }
}